package com.xingxuecn.limit.web.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.xingxuecn.limit.common.Constant;
import com.xingxuecn.limit.common.model.dto.User;
import com.xingxuecn.limit.common.model.vo.ResultModel;
import com.xingxuecn.limit.common.utils.ResponseUtils;

import org.apache.struts2.ServletActionContext;

/**
 * 登陆拦截器
 * @author Kris at 2018/9/14 15:59
 */
public class LoginInterceptor implements Interceptor {
    @Override
    public void destroy() {
        System.out.println("登陆拦截器销毁。。。");
    }

    @Override
    public void init() {
        System.out.println("登陆拦截器初始化。。。");
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        User user = (User) ServletActionContext.getRequest().getSession().getAttribute(Constant.USER_SESSION_KEY);
        if (user != null) {
            invocation.invoke();
            return null;
        }
        ResultModel resultModel = new ResultModel(401,"未登录或登陆以过期");
        ResponseUtils.writeJson(resultModel,ServletActionContext.getResponse());
        return null;
    }
}
